import template
import uix
import util
import uicls
import uiutil
import uiconst

def Make_Fill(d, **blah):
    align = template.AlignFromStr(d.get('align', 'fill'))
    newAlign = uiutil.GetAlign(align, 0, 0)
    return uicls.Fill(align=newAlign, color=template.Color(d.get('color', (1.0, 1.0, 1.0, 0.25))), width=d.get('width', 0), height=d.get('height', 0), state=template.StateFromStr(d.get('state', 'disabled')), left=d.get('left', 0), top=d.get('top', 0))



def Make_Label(d, **blah):
    styles = {'header': {'hspace': 2,
                'size': 10,
                'uppercase': True}}
    if ('style' in d):
        style = styles[d.style]
    else:
        style = {}

    def get(k, default):
        return d.get(k, style.get(k, default))


    defText = get('text', '')
    if mls.HasLabel(defText):
        defText = getattr(mls, defText)
    ret = uicls.Label(text=defText, parent=None, fontsize=get('size', 12), left=get('left', 0), top=get('top', 0), width=get('width', 0), height=get('height', 0), align=template.AlignFromStr(get('align', 'none')), autowidth=get('autoWidth', (not get('width', 0))), autoheight=get('autoHeight', (not get('height', 0))), position=template.PositionFromStr(get('position', 'default')), singleline=get('singleline', False), letterspace=get('hspace', 0), uppercase=get('uppercase', False), state=template.StateFromStr(get('state', 'pickChildren')))
    if get('bold', False):
        ret.mmbold = 0.5
    ret.anchors = template.AnchorsFromStr(get('anchors', 'left'))
    return ret



def Make_Icon(d, **blah):
    align = template.AlignFromStr(d.get('align', 'none'))
    anchors = template.AnchorsFromStr(d.get('anchors', 'left'))
    autoPos = template.PositionFromStr(d.get('position', 'default'))
    if (autoPos == uiconst.AUTOPOSCENTER):
        newAlign = uiconst.CENTERLEFT
    else:
        newAlign = uiutil.GetAlign(align, anchors, autoPos)
    ret = uicls.Icon(align=newAlign, left=d.get('left', 0), top=d.get('top', 0), width=d.get('width', d.get('size', 0)), height=d.get('height', d.get('size', 0)), state=template.StateFromStr(d.get('state', 'normal')), hint=(getattr(mls, d['hint']) if ('hint' in d) else None), icon=d.get('iconID', None), typeID=d.get('typeID', None), itemID=d.get('itemID', None), size=d.get('size', None), ignoreSize=True)
    return ret



def Make_Line(d, **blah):
    align = template.AlignFromStr(d.get('align', 'none'))
    anchors = template.AnchorsFromStr(d.get('anchors', 'left'))
    autoPos = template.PositionFromStr(d.get('position', 'default'))
    newAlign = uiutil.GetAlign(align, anchors, autoPos)
    return uicls.Line(parent=None, align=newAlign)


exports = util.AutoExports('template', locals())

